package com.gejt.test.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

@Component
@Slf4j
public class GraphQLClient {

    private final RestTemplate restTemplate;


    @Autowired
    public GraphQLClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String executeGraphQLQuery(String query) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        headers.add("Cookie", "_hjSession_3802852=eyJpZCI6ImQ2N2Y4M2ZlLWZmYzQtNDM5Mi1hNDZkLTk1NTc2MGViZWFmNSIsImMiOjE3MjY1NDQyODg0OTgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjoxLCJzcCI6MX0=; browser.timezone=Asia/Shanghai; browser.timezone.offset=8; _hjSessionUser_3802852=eyJpZCI6IjQxMDYxZTFmLTVjZGYtNThiOC04ZGYyLWYxM2JkMjFlMDQ2OSIsImNyZWF0ZWQiOjE3MjY1NDQyODg0OTAsImV4aXN0aW5nIjp0cnVlfQ==; currentUserId=119487; Authorization=a9e5d72d220c4f059b7493f37dd0b18d");
        headers.add("authority","ailaw-latest.easylawpro.com");
        headers.add("origin","https://ailaw-latest.easylawpro.com");
        headers.add("referer","https://ailaw-latest.easylawpro.com/lawyer");
        headers.add("sec-fetch-dest","empty");
        headers.add("sec-fetch-mode","cors");
        headers.add("sec-fetch-site","same-origin");

//        String requestJson = "{\"query\": \"" + query.replace("\"", "\\\"") + "\"}";
        String requestJson = "{\n" +
                "  \"operationName\": \"CaseSearch\",\n" +
                "  \"variables\": {\n" +
                "    \"isClose\": 0,\n" +
                "    \"sortBy\": \"CASEID\",\n" +
                "    \"sortOrder\": \"DESC\",\n" +
                "    \"startIndex\": 0,\n" +
                "    \"endIndex\": 20,\n" +
                "    \"selfUserId\": \"119487\"\n" +
                "  },\n" +
                "  \"query\": \"query CaseSearch($startIndex: Int!, $endIndex: Int!, $assigneeId: Int, $caseResult: String, $caseProgress: String, $caseType: String, $sortBy: SortBy, $sortOrder: SortOrder, $keywords: String, $caseTypeId: [Int], $caseId: [Int], $ailawCaseStatus: [Int], $selfUserId: Int!, $isClose: Int, $orderType: String, $vporderValue: [Int]) {\\n  result: caseSearch(\\n    query: {assigneeId: $assigneeId, caseResult: $caseResult, caseProgress: $caseProgress, caseType: $caseType, sortBy: $sortBy, sortOrder: $sortOrder, keywords: $keywords, caseTypeId: $caseTypeId, caseId: $caseId, ailawCaseStatus: $ailawCaseStatus, caseProgressIsClose: $isClose, orderType: $orderType, virtualParaOrderValue: $vporderValue}\\n  ) {\\n    numResult\\n    list: caseList(startIndex: $startIndex, endIndex: $endIndex) {\\n      id\\n      beneficiaryName\\n      petitionerProfile {\\n        id\\n        profileName\\n        __typename\\n      }\\n      beneficiaryProfile {\\n        id\\n        profileName\\n        __typename\\n      }\\n      caseProgress\\n      employee {\\n        id\\n        __typename\\n      }\\n      caseTemplateName\\n      caseTemplateDisplayName\\n      caseProgress\\n      assigneeList {\\n        id\\n        name\\n        __typename\\n      }\\n      caseResult\\n      createdOn\\n      todoFlag(selfUserId: $selfUserId)\\n      stepGroupsV2 {\\n        id\\n        profileId\\n        status\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n\"\n" +
                "}";

        HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
        log.info("Request JSON: {}", requestJson);
        ResponseEntity<String> response = restTemplate.postForEntity("https://ailaw-latest.easylawpro.com/api/glue", entity, String.class);
        log.info("Response: {}", response.getBody());
        return response.getBody();
    }
}
